package ru.edu.usu.cs.bubbles.engine.box2d.colliders;

import ru.edu.usu.cs.bubbles.engine.GameWorld;
import ru.edu.usu.cs.bubbles.engine.box2d.Box2dWhirlpool;

import android.util.Log;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Fixture;

public class Box2dWhirlpoolCollider extends Collider {
	public Box2dWhirlpoolCollider(GameWorld world) {
		super(world);
	}

	@Override
	public void processCollision(Fixture firstFixture, Fixture secondFixture) {
		Box2dWhirlpool whirlpool;
		Body contactedBody;
		
		if(firstFixture.getGameEntity() instanceof Box2dWhirlpool) {
			whirlpool = (Box2dWhirlpool) firstFixture.getGameEntity();
			contactedBody = secondFixture.getGameEntity().getBody();
		} else if(secondFixture.getGameEntity() instanceof Box2dWhirlpool) {
			whirlpool = (Box2dWhirlpool) secondFixture.getGameEntity();
			contactedBody = firstFixture.getGameEntity().getBody();
		} else {
			Log.e("CONTACT", "Wrong collider to contact: " 
					+ firstFixture.getGameEntity().getClass()
				    + " " + secondFixture.getGameEntity().getClass());
			return;
		}
		
		Vector2 contactedBodyCenter = contactedBody.getPosition();
		Vector2 forceVector =  new Vector2(contactedBodyCenter)
									.sub(whirlpool.getBody().getPosition());
		float forceVal = (forceVector.len() - whirlpool.radius)
							* whirlpool.power;
							
		Vector2 force = forceVector.mul(forceVal).rotate(whirlpool.forceAngle);		
		contactedBody.applyForce(force, contactedBodyCenter);
	}
}
